<?php
class Scraper {
	
	protected $curPos; 	// global cursorPosition
	protected $mustFindEnd; //bool
	protected $capturedData; // string, capturedStringId --> global temp
	
	protected $url; //string
	protected $startPattern; //string
	protected $endPattern; //string
	
	protected $offsetString; //string
	protected $maxToFind; // int
	
	protected $initialized ; // bool

	public function setMaxToFind($maxToFind) {
		$this->maxToFind = maxToFind;
	}

	protected $capturedList; //  vector of strings, list of captured items
	
    function __construct(){
		$this->capturedList = array();
		$this->maxToFind = 1;
		$this->url = "";
		$this->startPattern = "";
		$this->endPattern = ' ';
		$this->offsetString = "";
		$this->initialized = false;
	}
	
	public function getResultList() // return array of strings  getStringlist
	{
		return $this->capturedList;
	}
	
	public function getUrl() {
		return $this->url;
	}

	public function setUrl($url) {
		$this->url = $url;
	}

	public function getStartPattern() {
		return $this->startPattern;
	}

	public function setStartPattern($startPattern) {
		$this->startPattern = $startPattern;
	}

	public function getEndPattern() { // return char
		return $this->endPattern;
	}

	public function setEndPattern($endPattern /*char*/) {
		$this->endPattern = $endPattern;
	}

	public function getOffsetString() {
		return $this->offsetString;
	}

	public function setOffsetString($offsetString) {
		$this->offsetString = $offsetString;
	}

	
	public function initialize($url,$startPattern, $endPattern, $offsetString, $maxToFind ){
		
		$this->url = $url;
		$this->startPattern = $startPattern;
		$this->endPattern = $endPattern;
		$this->offsetString = $offsetString;
		$this->maxToFind = $maxToFind;
		
		$this->initialized = true;
		return true;
	}
	
    public function searchForData() // return bool
    {
    	if(!$this->initialized)
    		return false;
    	
        $songsFound = false;
  
		$inputBuffer = file_get_contents($this->url);
          
        $mustFindEnd = false;
        $capturedData = "";
        
        	// first look for tracklist
        	$curPos = 0;
        	$targetFound = false;
        	$lookForStrings = false;
			$inputPos = 0;
            
            while ( !$targetFound && $inputPos < strlen($inputBuffer))
            {         
				$charRead = $inputBuffer[$inputPos];
				if($charRead == $this->offsetString[$curPos]){
					//check if complete word found
					$curPos++;
					if($curPos >= strlen($this->offsetString) ){
						$targetFound = true;
						$curPos = 0;
					}						
				}
				else{
					$curPos = 0;
				}
				
                if($targetFound){
                	$lookForStrings = true;
                }
				 $inputPos++;
            }
            
        	// now look for Strings
            $curPos = 0;
            $capturedData = "";
            $targetFound = false;
			$this->capturedList = array();
            $modus = 0;
			$capturedString = "";
			if($lookForStrings){
            	
	            while ( count($this->capturedList) < $this->maxToFind && $inputPos < strlen($inputBuffer) )
	            {               
	            	$charRead = $inputBuffer[$inputPos];
					if($modus == 0){
						if($charRead == $this->startPattern[$curPos]){
							//check if complete word found
							$curPos++;
							if($curPos >= strlen($this->startPattern) ){
								$targetFound = true;
								$curPos = 0;
							}						
						}
						else{
							$curPos = 0;
						}
						
						if($targetFound){
							$modus = 1;
							$targetFound = false;
						}
					}
					// modus 1: capture String link
					else if($modus == 1){
						if("$charRead" != "$this->endPattern"){
							//check if complete word found
							$capturedString = "$capturedString" . "$charRead";
						}
						else{
							array_push($this->capturedList, $capturedString);
							$targetFound = true;
							$capturedString = "";
							$songsFound = true;
						}
						
						if($targetFound){
							$modus = 0;
							$targetFound = false;
						}
					}
					$inputPos++;
						
	                
	            }
            }
			
			return $songsFound;
     
    }
}

?>